﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lamex.Base;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;

namespace Lamex.Library.UserCotrolUI
{
    public partial class FrmSeatControlUI : UserControl
    {
        public event EventHandler UcDoubleClick;
        public FrmSeatControlUI()
        {
            InitializeComponent();
            chartSeatUI.DoubleClick += new EventHandler(chartSeatUI_DoubleClick);

        }
        private void chartSeatUI_DoubleClick(object sender, EventArgs e)
        {
            if (UcDoubleClick != null)
            {
                UcDoubleClick(null, e);
            }
        }
        /// <summary>
        /// 设置站别名称11
        /// </summary>
        /// <param name="ProcessName"></param>

        public void ShowProcessList(string ProcessCode, int maxStandtime,int minStandtime,int standetime,int productTime, string Name, ref bool IsValue,ref bool  ReValue)
        {
            try
            {
                if (string.IsNullOrEmpty(ProcessCode))
                {
                    XtraMessageBox.Show("没有输入工序号");
                    return;
                }

                #region"员工时间表"
                DataTable dtItme = BaseBuildSql.GetDataTableBySql(@" select a.* from  [dbo].[HR_Work_ShiftRec]  a 
 join (select top 1* from  [HR_Station_Info]  where STATION_NAME='" + Name + @"') b on b.DEPARTMENT=a.DEPARTMENT
 where   a.EFFECTIVE_DATE<  CONVERT(nvarchar(10),GETDATE(),120) and CONVERT(nvarchar(10),GETDATE(),120)<CONVERT(nvarchar(10), a. EXPIRY_DATE,120)
                                                                ");
                string time = DateTime.Now.ToString("yyyy -MM-dd");
                string startday = DateTime.Now.ToString("yyyy-MM-dd");
                string timestart1 = "";
                string timestart2 = "";
                string timestart3 = "";
                string timesEnd1 = "";
                string timesEnd2 = "";
                string timesEnd3 = "";
                if (dtItme.Rows.Count != 0)
                {
                    time = time + " " + dtItme.Rows[0]["T1_START"].ToString();
                    timestart1 = startday + " " + dtItme.Rows[0]["T1_START"].ToString();
                    timestart2 = startday + " " + dtItme.Rows[0]["T2_START"].ToString();
                    timestart3 = startday + " " + dtItme.Rows[0]["T3_START"].ToString();
                    timesEnd1 = startday + " " + dtItme.Rows[0]["T1_END"].ToString();
                    timesEnd2 = startday + " " + dtItme.Rows[0]["T2_END"].ToString();
                    timesEnd3 = startday + " " + dtItme.Rows[0]["T3_END"].ToString();

                }
                else
                {
                    XtraMessageBox.Show("员工没有排班");
                    return;
                }
                DateTime startTime1 = Convert.ToDateTime(timestart1);
                DateTime startTime2 = Convert.ToDateTime(timestart2);
                DateTime startTime3 = Convert.ToDateTime(timestart3);

                DateTime endTime1 = Convert.ToDateTime(timesEnd1);
                DateTime endTime2 = Convert.ToDateTime(timesEnd2);
                DateTime endTime3 = Convert.ToDateTime(timesEnd3);
                DateTime startTime = DateTime.Now;
                decimal hour1 = 0;
                decimal hour2 = 0;
                decimal hour3 = 0;
                #region"计算当前时间的数据值"
                if (endTime1 > startTime)
                {
                    TimeSpan span = startTime.Subtract(startTime1);
                    hour1 = Convert.ToDecimal(span.Hours) + Convert.ToDecimal(span.Minutes) / 60;
                }
                else if (endTime1 < startTime && startTime2 > startTime)
                {
                    TimeSpan span = endTime1.Subtract(startTime1);
                    hour1 = Convert.ToDecimal(span.Hours) + Math.Round(Convert.ToDecimal(span.Minutes) / 60, 1);
                }
                else if (startTime2 < startTime && startTime < endTime2)
                {
                    TimeSpan span = endTime1.Subtract(startTime1) + startTime.Subtract(startTime2);
                    hour1 = Convert.ToDecimal(span.Hours) + Math.Round(Convert.ToDecimal(span.Minutes) / 60, 1);
                }
                else if (endTime2 < startTime && startTime < startTime3)
                {
                    TimeSpan span = endTime1.Subtract(startTime1) + endTime2.Subtract(startTime2);
                    hour1 = Convert.ToDecimal(span.Hours) + Math.Round(Convert.ToDecimal(span.Minutes) / 60, 1);
                }
                else if (startTime3 < startTime && startTime < endTime3)
                {
                    TimeSpan span = endTime1.Subtract(startTime1) + endTime2.Subtract(startTime2) + startTime.Subtract(startTime3);
                    hour1 = Convert.ToDecimal(span.Hours) + Math.Round(Convert.ToDecimal(span.Minutes) / 60, 1);
                }
                else if (endTime3 < startTime)
                {
                    TimeSpan span = endTime1.Subtract(startTime1) + endTime2.Subtract(startTime2) + endTime3.Subtract(startTime3);
                    hour1 = Convert.ToDecimal(span.Hours) + Math.Round(Convert.ToDecimal(span.Minutes) / 60, 1);
                }
                decimal iStander = 3600 * hour1 / decimal.Parse(productTime.ToString());

                #endregion

                #endregion


                DataTable dtProcess = BaseBuildSql.GetDataTableBySql(@" select a.ngNo,  case when  a.wordtime< 32767 then case when a.wordtime=a.netime then 200 else a.netime -a.wordtime end  else  (65535-a.wordtime)+a.netime end as UseTime,writetime Uptime, ROW_NUMBER() over(order by writetime) as rows from
                                                                [Edgeband_Program].[dbo].[ST_S08_Receive_Data] a where a.ngNo = '" + ProcessCode + "' and  CONVERT(nvarchar(10), writetime, 120) = CONVERT (nvarchar(10),GETDATE(),120) and startIndex != 2 and wordtime != 0 and station_name='"+ Name + "' order by writetime  ");
                if (dtProcess.Rows.Count == 0)
                {
                    ReValue = false;
                    return;
                }
                ReValue = true;
                if (iStander > dtProcess.Rows.Count)
                    IsValue = false;
                else IsValue = true;
                string ProcessName = Name;
                chartSeatUI.Series.Clear();

                Series seriesAuto = new Series("", ViewType.Line);

                seriesAuto.DataSource = dtProcess;

                //seriesAuto.ArgumentScaleType = ScaleType.Qualitative;
                //名称
                seriesAuto.ArgumentDataMember = "rows";

                seriesAuto.ValueScaleType = ScaleType.Numerical;

                //seriesAuto.Visible = false;
                //取值字段 
                seriesAuto.ValueDataMembers.AddRange(new string[] { "UseTime" });
                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;
                //显示标项目名称和百分比数据
                //seriesAuto.PointOptions.PointView = PointView.ArgumentAndValues;
                seriesAuto.PointOptions.ValueNumericOptions.Format = NumericFormat.General;

                //seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
                //ChartTitle autoTitle = new ChartTitle();
                //autoTitle.Text = "Hour";
                //chartHour.Titles.Clear();
                //chartHour.Titles.Add(autoTitle);
                this.chartSeatUI.Series.Add(seriesAuto);
                XYDiagram diagramhour = (XYDiagram)chartSeatUI.Diagram;
                diagramhour.AxisY.ConstantLines.Clear();
                //diagramhour.EnableAxisXScrolling = true;
                //diagramhour.EnableAxisXZooming = true;
                //diagramhour.EnableAxisYScrolling = true;
                //diagramhour.EnableAxisYZooming = true;
                // diagramhour.AxisY.VisualRange.MinValue = minStandtime;
                //diagramhour.AxisY.VisualRange.MaxValue = 250;
                //diagramhour.AxisY.VisualRange.SideMarginsValue = 10;
                diagramhour.AxisY.WholeRange.MaxValue = 400;
                diagramhour.AxisY.WholeRange.MinValue = 50;
                diagramhour.AxisY.WholeRange.AlwaysShowZeroLevel = true;
                diagramhour.AxisY.WholeRange.SideMarginsValue = 1;

                //diagramhour.AxisY.Logarithmic = true;
                //diagramhour.AxisY.LabelVisibilityMode = AxisLabelVisibilityMode.Default;
                //diagramhour.AxisY.LogarithmicBase=10;
                // diagramhour.AxisX.WholeRange.MinValue = 30;
                //diagramhour.AxisX.Interlaced = true;
                //diagramhour.AxisX.Label = 10
                //diagramhour.AxisX.WholeRange.MaxValue= 150;
                //diagramhour.AxisX.WholeRange.MinValue = 0;
                //diagramhour.AxisX.Logarithmic = true;
                //diagramhour.AxisX.LogarithmicBase = 5;

                //ConstantLine constantLineligh = new ConstantLine("250", 250);
                //constantLineligh.Color = Color.AntiqueWhite; //直线颜色
                //constantLineligh.Title.TextColor = Color.Black;   //直线文本字体颜色    
                //constantLineligh.Title.Visible = true;
                //constantLineligh.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLineligh);

                //ConstantLine constantLinelow = new ConstantLine( (standetime).ToString(), standetime);
                //constantLinelow.Color = Color.AntiqueWhite; //直线颜色
                //constantLinelow.Title.TextColor = Color.Black;   //直线文本字体颜色    
                //constantLinelow.Title.Visible = true;
                //constantLinelow.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLinelow);

                //ConstantLine constantLine2 = new ConstantLine("CT:" + iStandTime.ToString(), iStandTime);
                //constantLine2.Color = Color.Green; //直线颜色
                //constantLine2.Title.TextColor = Color.Green;   //直线文本字体颜色    
                //constantLine2.Title.Visible = true;
                //constantLine2.ShowInLegend = false;
                //diagramhour.AxisY.ConstantLines.Add(constantLine2);

                ConstantLine constantLine3 = new ConstantLine("MAX:" + maxStandtime, maxStandtime);
                constantLine3.Color = Color.Red; //直线颜色
                constantLine3.Title.TextColor = Color.Red;   //直线文本字体颜色    
                constantLine3.Title.Visible = true;
                constantLine3.ShowInLegend = false;
                diagramhour.AxisY.ConstantLines.Add(constantLine3);

                ConstantLine constantLine4 = new ConstantLine("MIN:" + (minStandtime).ToString(), minStandtime);
                constantLine4.Color = Color.Gray; //直线颜色
                constantLine4.Title.TextColor = Color.Gray;   //直线文本字体颜色    
                constantLine4.Title.Visible = true;
                constantLine4.ShowInLegend = false;
                diagramhour.AxisY.ConstantLines.Add(constantLine4);
                diagramhour.AxisY.Title.Text = "";
                diagramhour.AxisY.Title.Visible = false;
                diagramhour.AxisY.Title.Alignment = StringAlignment.Center;
                diagramhour.AxisY.Title.Antialiasing = true;
                diagramhour.AxisY.Color = Color.Black;
                diagramhour.AxisX.Color = Color.Black;



                //diagramhour.AxisX.Title.Text = "icount";
                //diagramhour.AxisX.Title.Visible = true;
                //diagramhour.AxisX.Title.Alignment = StringAlignment.Center;
                //diagramhour.AxisX.Title.Antialiasing = true;
                //横标题设置
                //chartSeatUI.Titles.Clear();
                //ChartTitle titles = new ChartTitle();            //声明标题
                //titles.Text = ProcessName;                            //名称
                //                                                      //titles.TextColor = System.Drawing.Color;   //颜色
                //titles.Indent = 1;                                //设置距离  值越小柱状图就越大
                //titles.Font = new Font("Tahoma", 12, FontStyle.Bold);            //设置字体
                //titles.Dock = ChartTitleDockStyle.Top;           //设置对齐方式
                //titles.Indent = 0;
                //titles.Alignment = StringAlignment.Center;       //居中对齐
                //chartSeatUI.Titles.Add(titles);                 //添加标题  
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message + "seat");
            }


        }
    }
}
